#include "comm.h"

int main()
{
  /*-将默认的umask设置为0-*/
  umask(0);
  int mid = mkfifo(MY_FILE, 0666);
  if(mid != 0)
  {
    perror("mkfifo faile");
    exit(1);
  }

  int fd = open(MY_FILE, O_RDONLY);
  if(fd < 0)
  {
    perror("open faild");
    exit(2);
  }

  // 业务逻辑
  while(1)
  {
    char buffer[64] = {0};
    ssize_t s = read(fd, buffer, sizeof(buffer)-1);
    if(s > 0)
    {
      buffer[s] = 0;
      if(strcmp(buffer, "show") == 0)
      {
        if(fork() == 0)
        {
          execlp("ls", "ls", "-l", NULL);
          exit(1);
        }
        waitpid(-1, NULL, 0);
      }
      else if(strcmp(buffer, "run") == 0)
      {
        if(fork() == 0)
        {
          execlp("sl", "sl", NULL);
          exit(1);
        }
        waitpid(-1, NULL, 0);
      }
      else 
      {
        printf("client say to server -> %s\n", buffer);
      }
    }
    else if(s == 0)
    {
      printf("read end -> client quit\n");
      break;
    }
    else 
    {
      printf("read error\n");
      break;
    }
  }
  return 0;
}
